import { routeRedirectTo } from "@/utils/route"
import { getStorage } from "@/utils/tools"
import { useUser } from "@/store";
import { storeToRefs } from "pinia";

/**
数据验证（表单验证）
*/
var error = '';

export const checkLogin = (type: string | undefined = 'boolean') => {
	const { token } = storeToRefs(useUser());
	if (!token.value) {
		switch (type) {
			case 'boolean':
				return false;
				break;
			case 'login':
				routeRedirectTo('/pages/login/index')
				return false;
				break;
		}
	}
	return true;
}

export const check = (data: { [x: string]: string | any[]; }, rule: string | any[]) => {
	for (var i = 0; i < rule.length; i++) {
		if (!rule[i].checkType) {
			return true;
		}
		if (!rule[i].name) {
			return true;
		}
		if (!rule[i].errorMsg) {
			return true;
		}
		if (!data[rule[i].name]) {
			error = rule[i].errorMsg;
			return false;
		}
		switch (rule[i].checkType) {
			case 'custom':
				if (typeof rule[i].validate == 'function') {
					if (!rule[i].validate(data[rule[i].name])) {
						error = rule[i].errorMsg;
						return false;
					}
				}
				break;
			case 'required':
				var reg = new RegExp('/[\S]+/');
				if (reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'string':
				var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'int':
				var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
				break;
			case 'between':
				if (!isNumber(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				var minMax = rule[i].checkRule.split(',');
				minMax[0] = Number(minMax[0]);
				minMax[1] = Number(minMax[1]);
				if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'betweenD':
				var reg = /^-?[1-9][0-9]?$/;
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				var minMax = rule[i].checkRule.split(',');
				minMax[0] = Number(minMax[0]);
				minMax[1] = Number(minMax[1]);
				if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'betweenF':
				var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				var minMax = rule[i].checkRule.split(',');
				minMax[0] = Number(minMax[0]);
				minMax[1] = Number(minMax[1]);
				if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'same':
				if (data[rule[i].name] != rule[i].checkRule) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'notsame':
				if (data[rule[i].name] == rule[i].checkRule) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'email':
				var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'phoneno':
				var reg = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'zipcode':
				var reg = /^[0-9]{6}$/;
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'reg':
				var reg = new RegExp(rule[i].checkRule);
				if (!reg.test(String(data[rule[i].name]))) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'in':
				if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'notnull':
				if (Number(data[rule[i].name]) == 0 || data[rule[i].name] == undefined || data[rule[i].name] == null || data[rule[i].name]
					.length < 1) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'lengthMin':
				if (data[rule[i].name].length < rule[i].checkRule) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
			case 'lengthMax':
				if (data[rule[i].name].length > rule[i].checkRule) {
					error = rule[i].errorMsg;
					return false;
				}
				break;
		}
	}
	return true;
}
export const isNumber = (checkVal: string | number) => {
	var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
	return reg.test(String(checkVal));
}


export default {
	checkLogin,
	check,
	isNumber
}